<template>
  <div class="article-detail">
    <!-- 导航栏部分 -->
    <van-nav-bar
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <!-- 文章部分 -->
    <div class="article-wrap">
      <h3 class="title">{{ articleInfo.title }}</h3>
      <div class="aut_info">
        <van-image
          width="60"
          height="60"
          fit="cover"
          :src="articleInfo.aut_photo"
          round
        />
        <div class="info">
          <div>{{ articleInfo.aut_name }}</div>
          <div class="time">{{ articleInfo.pubdate | formatTime }}</div>
        </div>
        <div v-if="$store.state.userInfo.id != articleInfo.art_id">
          <van-button
            @click="doFollow(true)"
            v-if="!articleInfo.is_followed"
            type="info"
            round
            >+ 关注</van-button
          >
          <van-button @click="doFollow(false)" v-else type="primary" round
            >- 取关</van-button
          >
        </div>
      </div>

      <!-- 文章内容 -->
      <div class="content" v-html="articleInfo.content"></div>
      <!-- 分割线 -->
      <van-divider>end</van-divider>
      <!-- 功能按钮区域 -->
      <div class="btn-wrap">
        <van-button
          v-if="articleInfo.attitude == -1"
          @click="zan(true)"
          type="primary"
          plain
          round
          icon="good-job-o"
          >点赞</van-button
        >
        <van-button
          v-if="articleInfo.attitude == 1"
          @click="zan(false)"
          type="primary"
          plain
          round
          icon="good-job"
          >取消点赞</van-button
        >

        <div v-if="articleInfo.attitude == -1" style="width: 30px"></div>
        <van-button
          v-if="articleInfo.attitude == -1"
          @click="unlike(true)"
          type="danger"
          plain
          round
          icon="delete-o"
          >不喜欢</van-button
        >
        <van-button
          v-if="articleInfo.attitude == 0"
          @click="unlike(false)"
          type="danger"
          plain
          round
          icon="delete"
          >不喜欢</van-button
        >
      </div>
    </div>
    <!-- 评论列表 -->
    <div class="u_box">
      <div
        class="heads"
        v-for="(item, index) in commentaList"
        ::key="item.aut_id"
      >
        <van-image class="img" round fit="cover" :src="item.aut_photo" />
        <div class="right-sizi">
          <p class="heima">{{ item.aut_name }}</p>
          <span>{{ item.content }}</span>
          <span @click="showComment(item.com_id, index)"
            ><i>{{ item.pubdate | formatTime }}</i> 回复({{
              item.reply_count
            }})</span
          >
        </div>
        <van-icon class="rigth_icon" name="like-o" />
      </div>
    </div>

    <!-- 评论部分 -->
    <van-field
      v-model="comment"
      class="van_fi"
      center
      clearable
      placeholder="请输入评论"
    >
      <template #button>
        <van-button size="small" type="primary" @click="pub">发表</van-button>
      </template>
    </van-field>
    <!-- <comment></comment> -->
    <!-- 回复评论部分 -->
    <van-action-sheet v-model="show" title="发表回复">
      <div class="heads">
        <van-image
          class="img"
          round
          fit="cover"
          :src="commentaList[info].aut_photo"
        />
        <div class="right-sizi">
          <p class="heima">{{ commentaList[info].aut_name }}</p>
          <span>{{ commentaList[info].content }}</span>
          <span>{{ commentaList[info].pubdate | formatTime }}</span>
        </div>
      </div>

      <van-divider dashed>回复列表</van-divider>

      <div v-for="item in responselist" :key="item.aut_id" class="heads">
        <van-image class="img" round fit="cover" :src="item.aut_photo" />
        <div class="right-sizi">
          <p class="heima">{{ item.aut_name }}</p>
          <span>{{ item.content }}</span>
          <span>{{ item.pubdate | formatTime }}</span>
        </div>
      </div>
      <van-divider class="van_divs" dashed>没有更多了</van-divider>
      <!-- 发表评论部分 -->
      <van-field
        v-model="postReq"
        class="van_fi"
        center
        clearable
        placeholder="请输入评论"
      >
        <template #button>
          <van-button size="small" type="primary" @click="pusReq"
            >发表</van-button
          >
        </template>
      </van-field>
    </van-action-sheet>
  </div>
</template>

<script>
import { articleDetailsAPI, commentDetailAPI, addCommentAPI } from "@/api";
export default {
  async created() {
    //获取文章内容并渲染
    let res = await articleDetailsAPI(this.$route.query.back);
    this.articleInfo = res.data.data;
    //获取评论信息并渲染
    let comm = await commentDetailAPI({
      type: "a",
      source: res.data.data.art_id,
    });
    this.commentaList = comm.data.data.results;
  },
  data() {
    return {
      articleId: this.$route.query.id,
      articleInfo: {},
      articleContent: "",
      commentaList: {},
      comment: "",
      show: false,
      responselist: {},
      info: 0,
      postReq: "",
      id: "",
    };
  },
  methods: {
    async pub() {
      let res = await addCommentAPI({
        target: this.$route.query.back,
        content: this.comment,
      });
      this.comment = "";
      this.commentaList.push(res.data.data.new_obj);
    },
    async showComment(source, index) {
      this.info = index;
      this.show = true;
      let res = await commentDetailAPI({
        type: "c",
        source,
      });
      this.responselist = res.data.data.results;
      this.id = source;
    },
    async pusReq() {
      let res = await addCommentAPI({
        target: this.id,
        content: this.postReq,
        art_id: this.$route.query.back,
      });
      this.responselist.push(res.data.data.new_obj);
      this.postReq = "";
      //发布完评论后,在调用一次加载评论的接口
      //获取评论信息并渲染
      let comm = await commentDetailAPI({
        type: "a",
        source: res.data.data.art_id,
      });
      this.commentaList = comm.data.data.results;
    },
  },
};
</script>

<style lang="less" scoped>
.article-detail {
  font-size: 16px;
  margin-top: 60px;
  // 文章部分
  .article-wrap {
    padding: 0 20px;
    .title {
      font-size: 20px;
      margin: 10px 0;
    }

    .aut_info {
      height: 80px;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      .info {
        margin-left: 20px;
        // 设置剩余空间里的占比，如果就只给它设置了
        // 就代表剩余部分全部给它
        flex: 1;

        .time {
          color: gray;
        }
      }
    }
    .content {
      // 如果最后一个单词不够放了，就把整个单词换一行开始写
      word-wrap: break-word;
      line-height: 32px;
      // word-break: break-all; 以一行实在放不下了就换行可能会导致单词脱节
      overflow: hidden;

      ::v-deep {
        // 这个标签是一个行内元素，所以设置宽度没用
        pre {
          code {
            display: block;
            width: 100%;
            overflow: auto;
            background-color: #101010;
            color: #dcdcdc;
          }
        }

        img {
          width: 80%;
        }
      }
    }
    .btn-wrap {
      display: flex;
      justify-content: center;
      //   margin-bottom: 60px;
      .van-button {
        width: 120px;
      }
    }
  }
}
.van_fi {
  position: fixed;
  bottom: 0;
  left: 0;
}
.u_box {
  margin-bottom: 60px;
}
.heads {
  height: 84px;
  //   margin-bottom: 60px;
  margin-top: 10px;
  display: flex;
  //   justify-content: center;
  .img {
    width: 45px;
    height: 45px;
    margin-left: 10px;
  }
  .rigth_icon {
    flex: 1;
    margin-left: 150px;
    margin-top: 10px;
    font-size: 16px;
  }
  .right-sizi {
    display: flex;
    justify-content: center;
    // align-items: center;
    flex-direction: column;
    margin-left: 15px;

    p {
      color: #466b9d;
    }
    span {
      color: #000;
      i {
        color: grey;
      }
    }
  }
}
.van_divs {
  margin-top: 20px;
  margin-bottom: 60px;
}
</style>
